// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.fowo.api.entity.SupplierFlowNode;
import com.fowo.api.entity.SupplierTaskInstance;
import com.fowo.api.model.supplier.flow.node.SupplierFlowNodeEnum;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商产品开发实施 服务实现类
 */
@Service
@Slf4j
public class SupplierTaskInstanceServiceImpl
        extends SupplierTaskInstanceBaseServiceImpl {
    @Resource
    private SupplierFlowNodeServiceImpl supplierFlowNodeService;

    /**
     * 完成
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了完成操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doFinish(List<Long> ids) throws Exception {  //1:草稿 2:已提交 3:已接受 4:执行中 5:已通过 6:已完成 -1:已撤回
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierTaskInstance> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, node.getNode(), CollUtil.newArrayList(node.getNodeUp()), item -> {
        });
        this.updateBatchById(list);
    }


    /**
     * 重新编辑
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了重新编辑操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void unDoFinish(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierTaskInstance> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, node.getNodeUp(), CollUtil.newArrayList(node.getNodeUp()), item -> {
        });
        this.updateBatchById(list);
    }

    /**
     * 转发
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了转发操作"
    )
    @Override
    public void doForward(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        List<SupplierTaskInstance> list = this.listByIds(ids);
        SupplierFlowNode node = supplierFlowNodeService.getConditionNode(list);
        supplierFlowNodeService.process(list, node, SupplierFlowNodeEnum.转发.getValue(), CollUtil.newArrayList(node.getNodeUp()), item -> {
        });
        this.updateBatchById(list);
    }
}
